import Vue from 'vue'
import VueRouter from 'vue-router'


import Register from '../components/Register.vue'

//Home子路由

Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        
        component: ()=>import('../components/Login.vue')
    },
    {
        path: '/home',

        component: ()=>import('../components/Home.vue'),

        //路由重定向子路由 /welcome
        redirect:'/welcome',

        children:[
            {
                path:'/welcome',
                component:()=>import('../components/Welcome.vue')
            },
            {
                path:'/users',
                component:()=>import('../components/user/Users.vue')
            },
            {
                path:'/rights',
                component:()=>import('../components/power/Rights.vue')
            },
            {
                path:'/roles',
                component:()=>import('../components/power/Roles.vue')
            },
            {
                path:'/categories',
                component:()=>import('../components/goods/Cate.vue')
            }
            
        ]
       
    },
    {
        path: '/register',
        component: Register
    },
]

const router = new VueRouter({
        routes
    })
    //挂载路由导航守卫
   router.beforeEach((to,from,next)=>{
       if(to.path=='/login') next();
       //获取token 
       const  tokenStr = window.sessionStorage.getItem("token");
       if(!tokenStr) next('/login');
       next();
   })
export default router